home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / tjoop11.zip / DLISTOBJ.PAS < prev    next >
Pascal/Delphi Source File  |  1991-05-17  |  8KB  |  334 lines

  1. UNIT DListOBJ ;
  2.  
  3.  
  4.  
  5.  
  6. {**********************************************************************
  7.  *                                                                    *
  8.  *  PROGRAM :  DListOBJ                PC FILE NAME :  DListOBJ.PAS   *
  9.  *  ----------------------------------------------------------------  *
  10.  *  LIBRARY MODULES USED :  DOS CRT                                   *
  11.  *  BaseType :  TP6.0 TPU SOURCE: BASETYPE.PAS  BINARY:  BASETYPE.TPU *
  12.  *  ----------------------------------------------------------------  *
  13.  *  PURPOSE :  The purpose of this UNIT is to provide a Doublely-     *
  14.  *             Linked list object that uses a common base and base    *
  15.  *             element type.                                          *
  16.  *  ----------------------------------------------------------------  *
  17.  *  AUTHOR   :  Thomas E. Jenkins, Jr.                                *
  18.  *              PROGRAMMER, UNIVERSITY OF SOUTH CAROLINA, USA         *
  19.  *  BITNET   :  C0361@UNIVSCVM.BITNET                                 *
  20.  *  INTERNET :  C0361@univscvm.csd.scarolina.EDU                      *
  21.  *              tomj@csdserver3.csd.scarolina.EDU                     *
  22.  *  ----------------------------------------------------------------  *
  23.  *  DISCAIMER :  This program has been tested to the best of my       *
  24.  *               abilities.  The author claims no responsibility      *
  25.  *               for the performance or side effects this program     *
  26.  *               may yield.                                           *
  27.  *  ----------------------------------------------------------------  *
  28.  *  DISTIBUTION :  This program is given freely to the PD realms.     *
  29.  *                 It may freely be copied and distributed.  Any      *
  30.  *                 one wishing to use some or whole parts of this     *
  31.  *                 program for commercial use please contact the      *
  32.  *                 author first.                                      *
  33.  *                                                                    *
  34.  **********************************************************************}
  35.  
  36.  
  37.  
  38.  
  39.                                   INTERFACE
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.              {
  47.  
  48.  
  49.                  Here is a basic view of the DList object tree:
  50.  
  51.                  TObject                    [ to allow use with streams   ]
  52.                     |
  53.                     |
  54.                     \___ TBaseOBJ           [ basic functions of Queue &  ]
  55.                              |              [ elements                    ]
  56.                              |
  57.                              \___ TElementOBJ    [ basic functionality of ]
  58.                              /         |         [ any element            ]
  59.                              |         |
  60.                              |         \___ TStrOBJ [ string specific     ]
  61.                              |         /            [ functions & storage ]
  62.                              |         |
  63.                              |         |
  64.                              |         \___ TIntOBJ [ integer specific    ]
  65.                              |         /            [ functions & storage ]
  66.                              |         |
  67.                              |         |
  68.                              |         \___ TRealOBJ [ Real specific      ]
  69.                              |                       [ functions & storage]
  70.                              |
  71.                              |
  72.                              \___ TDListOBJ      [ DList specific         ]
  73.                                                  [ functions & storage    ]
  74.  
  75.              }
  76.  
  77.  
  78. USES
  79.        BaseTypes ;
  80.  
  81.  
  82.  
  83. TYPE
  84.  
  85.        PDListOBJ                       = ^TDListOBJ ;
  86.        TDListOBJ                       = OBJECT ( TBaseOBJ )
  87.  
  88.          current                       : PElementOBJ ;
  89.  
  90.          CONSTRUCTOR Init ;
  91.  
  92.          PROCEDURE   Add (     item    : PElementOBJ ) ;
  93.            VIRTUAL ;
  94.  
  95.          FUNCTION    NextOne           : BOOLEAN ;
  96.            VIRTUAL ;
  97.  
  98.          FUNCTION    PrevOne           : BOOLEAN ;
  99.            VIRTUAL ;
  100.  
  101.          FUNCTION    Get               : PElementOBJ ;
  102.            VIRTUAL ;
  103.  
  104.          PROCEDURE   Delete ;
  105.            VIRTUAL ;
  106.  
  107.          FUNCTION    Empty             : BOOLEAN ;
  108.            VIRTUAL ;
  109.  
  110.          FUNCTION    TheType           : WORD ;
  111.            VIRTUAL ;
  112.  
  113.          PROCEDURE   SetData (     d   : POINTER ) ;
  114.            VIRTUAL ;
  115.  
  116.          DESTRUCTOR  Done ;
  117.            VIRTUAL ;
  118.  
  119.          END ;  {  TDListOBJ  }
  120.  
  121.  
  122.  
  123.  
  124.                                 IMPLEMENTATION
  125.  
  126.  
  127.  
  128.  
  129.  CONSTRUCTOR TDListOBJ.Init ;
  130.  
  131.    BEGIN  {  TDListOBJ.Init  }
  132.  
  133.      current := NIL ;
  134.  
  135.      END ;  {  TDListOBJ.Init  }
  136.  
  137.  
  138.  
  139.  
  140.  PROCEDURE   TDListOBJ.Add (     item  : PElementOBJ ) ;
  141.  
  142.   BEGIN  {  TDListOBJ.Add  }
  143.  
  144.     IF ( current = NIL )
  145.      THEN
  146.       BEGIN
  147.  
  148.         current := item ;
  149.  
  150.         Exit ;
  151.  
  152.         END ;  {  THEN  }
  153.  
  154.     IF ( current^.next = NIL )
  155.      THEN
  156.       BEGIN
  157.  
  158.         item^.last := current ;
  159.         current^.next := item ;
  160.  
  161.         current := item ;
  162.  
  163.         Exit ;
  164.  
  165.         END ;  {  THEN  }
  166.  
  167.     current^.next^.last := item ;
  168.     item^.next := current^.next ;
  169.     item^.last := current ;
  170.     current^.next := item ;
  171.  
  172.     current := item ;
  173.  
  174.     END ;  {  TDListOBJ.Add  }
  175.  
  176.  
  177.  
  178.  
  179.  FUNCTION    TDListOBJ.Get                       : PElementOBJ ;
  180.  
  181.   BEGIN  {  TDListOBJ.Get  }
  182.  
  183.     Get := current ;
  184.  
  185.     IF ( Empty )
  186.      THEN
  187.         Exit ;
  188.  
  189.     Delete ;
  190.  
  191.     END ;  {  TDListOBJ.Get  }
  192.  
  193.  
  194.  
  195.  PROCEDURE   TDlistOBJ.Delete ;
  196.  
  197.    BEGIN  {  TDlistOBJ.Delete  }
  198.  
  199.     IF ( ( current^.next = NIL ) AND
  200.          ( current^.last = NIL ) )
  201.      THEN
  202.       BEGIN
  203.  
  204.         current := NIL ;
  205.  
  206.         Exit ;
  207.  
  208.         END ;  {  THEN  }
  209.  
  210.      IF ( current^.next = NIL )
  211.       THEN
  212.        BEGIN
  213.  
  214.          current^.last^.next := NIL ;
  215.          current := current^.last ;
  216.  
  217.          Exit ;
  218.  
  219.          END ;  {  THEN  }
  220.  
  221.      IF ( current^.last = NIL )
  222.       THEN
  223.        BEGIN
  224.  
  225.          current^.next^.last := NIL ;
  226.          current := current^.next ;
  227.  
  228.          Exit ;
  229.  
  230.          END ;  {  THEN  }
  231.  
  232.      current^.next^.last := current^.last ;
  233.      current^.last^.next := current^.next ;
  234.  
  235.      current := current^.next ;
  236.  
  237.      END ;  {  TDlistOBJ.Delete  }
  238.  
  239.  
  240.  
  241.  
  242.  FUNCTION    TDListOBJ.Empty                     : BOOLEAN ;
  243.  
  244.   BEGIN  {  TDListOBJ.Empty  }
  245.  
  246.     Empty := ( current = NIL ) ;
  247.  
  248.     END ;  {  TDListOBJ.Empty  }
  249.  
  250.  
  251.  
  252.  
  253.  FUNCTION    TDlistOBJ.TheType         : WORD ;
  254.  
  255.    BEGIN  {  TDlistOBJ.TheType  }
  256.  
  257.      TheType := current^.TheType ;
  258.  
  259.      END ;  {  TDlistOBJ.TheType  }
  260.  
  261.  
  262.  
  263.  PROCEDURE   TDlistOBJ.SetData (     d : POINTER ) ;
  264.  
  265.    BEGIN  {  TDlistOBJ.SetData  }
  266.  
  267.      current^.SetData ( d ) ;
  268.  
  269.      END ;  {  TDlistOBJ.SetData  }
  270.  
  271.  
  272.  
  273.  
  274.  DESTRUCTOR  TDListOBJ.Done ;
  275.  
  276.    BEGIN  {  TDListOBJ.Done  }
  277.  
  278.      IF ( Empty )
  279.       THEN
  280.          Exit ;
  281.  
  282.      WHILE ( PrevOne )
  283.       DO ;
  284.  
  285.      WHILE ( NOT ( Empty ) )
  286.       DO
  287.          Dispose ( Get , Done ) ;
  288.  
  289.      current := NIL ;
  290.  
  291.      END ;  {  TDListOBJ.Done  }
  292.  
  293.  
  294.  
  295.  
  296.  FUNCTION    TDListOBJ.PrevOne         : BOOLEAN ;
  297.  
  298.   BEGIN  {  TDListOBJ.PrevOne  }
  299.  
  300.     PrevOne := FALSE ;
  301.  
  302.     IF ( current^.last = NIL )
  303.      THEN
  304.         Exit ;
  305.  
  306.     current := current^.last ;
  307.  
  308.     PrevOne := TRUE ;
  309.  
  310.     END ;  {  TDListOBJ.PrevOne  }
  311.  
  312.  
  313.  
  314.  
  315.  FUNCTION    TDListOBJ.NextOne         : BOOLEAN ;
  316.  
  317.   BEGIN  {  TDListOBJ.NextOne  }
  318.  
  319.     NextOne := FALSE ;
  320.  
  321.     IF ( current^.next = NIL )
  322.      THEN
  323.         Exit ;
  324.  
  325.     current := current^.next ;
  326.  
  327.     NextOne := TRUE ;
  328.  
  329.     END ;  {  TDListOBJ.NextOne  }
  330.  
  331.  
  332.  
  333.  
  334. END .